const app = getApp()
import {
  listSearch,
} from '../../api/index'
import {
  customerYf,taskList,acquireAnchor,shenyeDouyinRecommendList
} from '../../api/integralTask'
const handleAuthorize = require('../../utils/handleAuthorize')
Page({
  data: {
    isFocus: false, //聚焦
    searchTxtShow: false,
    searchTxt: '', //搜索内容
    inputValue: '', //输入内容
    excludeIdList: [], //排除查询id集合
    dataList: [], //
    pageSize: 10,
    pageNum: 1,
    listTotal: 0, //列表总数
    isBottom: false,//是否到底
    listNull: false, //列表是否为空
    resList:[],
    yfNum: 0,
    lat:'',
    lnt:'',
    isToken:false, //是否登录
    taskLs:[], //任务列表
  },
  onLoad: function (options) {
    // tt.setStorageSync('token', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoiWGZQbzU1Q1ZtZWp2blYxWStQK05oYzdubEtCNmx5cUxwZ0gvUTdHSS9qUFdUbnpsNFVZVm1oZXpVTis5Y3hkUiJ9.jTBr-pu-sf_tH3NIUuUzh0JKSYfKUauhj5aMNkvyRIU');
    
  },
  onShow(){
    this.setData({
      isToken:handleAuthorize.validate_login()
    })
    if(this.data.isToken){
      this.getCustomerYf()
    }
    this.getTaskList()
    if (this.data.lat == '') {
      this.getLatLnt()
    }else
      this.getList()
  },
  //积分规则(椰分)
  integralRule(){
    tt.navigateTo({
      url: '/pages/subPackages/pagesB/integralStrategy/integralStrategy?type=1',
    });
  },
  //积分明细跳转
  pointsDetails(){
    tt.navigateTo({
      url: '../subPackages/pagesC/pointsDetails/pointsDetails',
    });
  },
  //店铺详情跳转
  jumpShopDetail(e) {
    let item = e.currentTarget.dataset.item
    tt.navigateTo({
      url: '/pages/subPackages/pagesA/shopDetail/shopDetail?id=' + item.shopId,
    });
  },
  //跳转卡详情
  jumpcardDetail(e) {
    let item = e.currentTarget.dataset.item
    tt.navigateTo({
      url: '/pages/subPackages/pagesA/cardDetail/cardDetail?id=' + item.id + '&shopId=' + item.shopId,
    });
  },
  //获取椰分详情
  async getCustomerYf(){
    let res = await customerYf()
    this.setData({
      yfNum: res.result
    })
  },
  //获取任务列表
  async getTaskList(){
    let res = await taskList()
    this.setData({
      taskLs: res.result
    })
  },
  //点击任务
  clickView(e){
    let item = e.currentTarget.dataset.item
    if(!this.data.isToken||item.taskType==1){
      const childComponent = this.selectComponent('#loginPopupId');
      childComponent.openPopup();
    }else if(item.taskType==2){
      tt.followAwemeUser({
        awemeId: item.taskParams,
        success: (res) => {
          console.log("引导关注抖音号成功，已关注:", res.followed);
          this.setAcquireAnchor(item.taskParams)
        },
        fail: (res) => {
          console.log("引导关注抖音号失败，错误信息:", res);
        },
      });
      
    }
  },
  //关注领取奖励
  async setAcquireAnchor(taskParams){
    let data = {
      taskParams: taskParams
    }
    let res = await acquireAnchor(data)
    // tt.showToast({
    //   title: '关注成功',
    //   icon:'none'
    // });
    this.getTaskList()
  },
  //加入粉丝群回调
  handleJoinGroup(e){
    console.log(e.detail)
    this.getTaskList()
  },
  // 取消登录
  cancelFunc(){},
  // 确认登录
  confirmFunc(){
    handleAuthorize.jumpPublicLogin()
  },
  //获取列表
  async getList() {
    let parameter = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
      lnt:this.data.lnt,
      lat:this.data.lat,
    }
    let res = await shenyeDouyinRecommendList(parameter);
    this.data.resList = res.result.records
    if (this.data.dataList.length >= 10) {
      this.data.dataList = this.data.dataList.concat(res.result.records)
    } else {
      this.data.dataList = res.result.records
    }

    if (this.data.resList.length >= 10) {
      this.data.isBottom = false
    } else {
      this.data.isBottom = true
    }

    if(this.data.dataList.length==0){
      this.data.listNull = true
    }else{
      this.data.listNull = false
    }
    this.setData({
      isBottom: this.data.isBottom,
      dataList: this.data.dataList,
      listNull: this.data.listNull
    })
    setTimeout(()=>{
      tt.stopPullDownRefresh()
    },800)
  },
  // 获取定位经纬度
  async getLatLnt() {
    // console.log('handleAuthorize',handleAuthorize)
    let infoLocation = {}
    try {
      infoLocation = await handleAuthorize.getLocation()
      this.setData({
        lat: infoLocation.latitude,
        lnt: infoLocation.longitude,
      })     
      this.getList()      
    } catch (e) {
      this.getList()      
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.resList.length>=10){
      let namesArray = this.data.dataList.map(item => item.id);
      this.data.excludeIdList = namesArray
      this.getList()
    }else{
      
    }
  },
  // 页面下拉刷新
  onPullDownRefresh() {
    this.data.pageNum = 1
    this.getList()
    if(this.data.isToken){
      this.getCustomerYf()
    }
  },
})